interface SimplejsonOption{
	hideBoolean?: boolean;
}

class TreeViewJson extends TreeView {
	override childrenCreated(): void {
		super.childrenCreated();
		this.addEventListener(TreeView.spaceEvent, this.onSpace, this);
	}

	onSpace(ev: egret.Event): void {
		const item: TreeViewItem = ev.data;
		if (item.isFolder()) { return; }
		MainFrame.ins().leftWin.showAttr();
	}

	override onShowAttr(item: TreeViewItem): any {
		return {
			[item.getContent()]: item.data.param,
		};
	}

	override onAttrEnter(titem: TreeViewItem, item: AttrItem): void {
		titem.updateJsonValue(item.getValueText());
		this.onSave();
	}

	onSave(): void {}

	simplejson: any

	simplejsonOpt: SimplejsonOption

	simplejsonName: string;

	refreshSimpleJson(): void {
		this.constructorSimpleJson(this.simplejsonName, this.simplejson, this.simplejsonOpt);
	}

	// k,v 直接作为节点来使用！
	constructorSimpleJson(jname: string, data: any, opt: SimplejsonOption = {}): void {
		egret.assert(typeof (data) === 'object');
		this.simplejson = data;
		this.simplejsonName = jname;
		this.simplejsonOpt = opt;

		function obj2items(obj: any, name: string, _id: number): any {
			let id = _id;
			const item = { id: id++, content: name, items: [] };
			if (obj == null) {
				item.items = null;
				return item;
			}

			if (Array.isArray(obj)) {
				let key = 0;
				for (const v of obj) {
					if (Array.isArray(v)) {
						// donothing...
						item.items.push(obj2items(v, String(key), id++));
					} else if (typeof (v) === 'object') {
						item.items.push(obj2items(v, String(key), id++));
					} else {
						item.items.push({ id: id++, content: key, param: v });
					}

					key++;
				}
			} else if (typeof (obj) === 'object') {
				// for (const key in obj) {
				for (const [key, v] of Object.entries(obj)) {
					// const v = obj[key];
					if (Array.isArray(v)) {
						// donothing...
						item.items.push(obj2items(v, key, id++));
					} else if (typeof (v) === 'object') {
						item.items.push(obj2items(v, key, id++));
					} else {
						const data2: any = { id: id++, content: key, param: v };
						if (opt.hideBoolean && typeof (v) === 'boolean') {
							data2.hide = !v;
							data2.content = key;
						}
						item.items.push(data2);
					}
				}
			}

			return item;
		}
		const out = obj2items(data, jname, 0);
		this.constructorArray([out]);
	}

	// update display and json data!
	updateJson(key: string, value: any): void {
		// if (!this.simplejson)
		// 	return
		// this.simplejson[key] = value;
		// let item = this.findContent(key)
		// if (item){
		// 	item.setParam(value)
		// }
		// this.onUpdatedKeys(key,value)
		this.updateJsonKeys(null, key, value);
	}

	updateJsonKey(from: any, key: string, value: any): void {
		this.updateJsonKeys(from, key, value);
	}

	updateJsonKeys(from: any, key: string, value: any): void {
		if (!this.simplejson) { return; }
		miscUtils.updateJsonByKeys(this.simplejson, key, value);
		const item = this.findItemByKeys(key); //
		if (item) { item.setParam(value); }
		// else
		// 如果添加了group就不好办，只能重新加载json，
		// 为什么注释，因为也有可能是没有创建完毕
		//	this.refreshSimpleJson()
		this.onUpdatedKeys(key, value);
	}

	onUpdatedKeys(keys: string, value: string): void {} // override...

	override updateItemJson(item: TreeViewItem, value: string): void {
		// ui.msglog("updateItemJson",item.getContent(),value)

		const names = [];
		let i: egret.DisplayObject = item;
		while (i.parent) {
			i = i.parent;
			if (i === this.getRoot()) { break; }
			if (i instanceof TreeViewItem) { names.unshift(i.getContent()); }
		}

		// ui.msglog("names",names.concat(" "))

		let data = this.simplejson;
		for (const name of names) {
			data = data[name];
		}
		names.push(item.getContent());
		data[item.getContent()] = Utils.try2NumberFirstnot0(value);
		this.onUpdatedKeys(names.join('.'), value);
	}
}
